﻿#region Imports
using System;
using System.Collections.ObjectModel;
using System.Linq;
using CommonUtilities.Extensions;
using CommonUtilities.Util;
#endregion

namespace DirectoryAnalyzer.Models
{
    public class DuplicateFiles
    {
        #region Properties
        private Guid guid;
        public string GUid
        {
            get
            {
                if (IsFileEmpty || (IsThumbnail && IncludeThumbs)) guid = new Guid();
                else
                    guid = new Guid(DateTime.Now.ToString("yyyyMMddHHmmssff").ToCharArray()
                        .Select(chr => (byte)chr).ToArray()); //DateTime.Now.ToString("yyyyMMddHHmmss"); // RKD : make out smthn meaningful

                return guid.ToString();
            }
        }

        public long? ByteSize
        {
            get
            {
                if (Duplicates._Where(info => info.IsFileEmpty)._Any())
                    return 0;
                else if (Duplicates._Where(info => info.IsThumbnail)._Any())
                    return null;
                else
                    return Duplicates._Select(dup => dup.ByteSize).GetValue();
            }
        }

        public bool IsFileEmpty
        {
            get { return Duplicates._Where(info => info.IsFileEmpty)._Any(); }
        }

        public bool IsThumbnail
        {
            get { return Duplicates._Where(info => info.IsThumbnail)._Any(); }
        }

        public string Header { get { return FormatResultItemHeaders(Duplicates); } }
        public bool IsResultExpanded { get; set; }
        public bool IncludeThumbs { private get; set; }
        public string FileHash { get { return Duplicates.Select(dup => dup.HashCode).GetValue(); } }
        public string FileSize { get { return ByteSize.ToPracticalSize(); } }
        public long FileCount { get { return Duplicates.GetCount(); ; } }
        public ObservableCollection<FDupInfo> Duplicates { get; set; }
        #endregion

        #region Constructors
        public DuplicateFiles(bool includeThumbs)
        {
            this.IncludeThumbs = includeThumbs;
            this.IsResultExpanded = true;
        }
        #endregion

        #region Methods
        private string FormatResultItemHeaders(ObservableCollection<FDupInfo> duplicates)
        {
            FDupInfo file = duplicates.FirstOrDefault();
            FDupInfo fileHash = duplicates.Where(dup => dup.HashCode != HashUtil.DEFAULT_HASH).FirstOrDefault();
            if (file.IsNull()) return "";

            string fileName = file.FileName;
            string fileSize = file.FileSize;
            string hashCode = fileHash.IsNull() ? HashUtil.DEFAULT_HASH : fileHash.HashCode;

            if (duplicates.Where(info => info.IsFileEmpty).Any())
                return "Empty files";
            else if (duplicates.Where(info => info.IsThumbnail).Any() && IncludeThumbs)
                return "Thumbnail files";
            else
                return "File :: '" + fileName + "'; Size :: " + fileSize + "; Count :: " + FileCount
                    + "\nGUID :: " + GUid
                    + "\nHash :: '" + hashCode + "'";
        }
        #endregion
    }
}
